//
// Created by wangwanlong on 2024/10/29.
//

#ifndef MKAHYPAR_TIMER_H
#define MKAHYPAR_TIMER_H
#include <chrono>
#include <string>
namespace mkahypar{
  class Timer {
  public:
    Timer() = default;

    ~Timer() = default;

    Timer &operator=(const Timer &) = delete;

    Timer &operator=(Timer &&) = delete;

    static Timer &getInstance() {
      static Timer instance;
      return instance;
    }

    //设置时间
    void setTime(int maxTime){
      MAXTIME = maxTime;
      start_time = std::chrono::high_resolution_clock::now();
      end_time = start_time + std::chrono::seconds(maxTime);
    }



    void setMaxTime(int max_time) {
      MAXTIME = max_time;
    }

    //获取当前系统时间
    std::string getData(){
      std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
      time_t t = std::chrono::system_clock::to_time_t(now);
      char buff[32];
      strftime(buff, 32, "%Y-%m-%d %H:%M:%S", localtime(&t));
      return std::string(buff);
    }

    int getTime() {
      //return (clock() - start_time) / CLOCKS_PER_SEC;
      auto end = std::chrono::high_resolution_clock::now();
      auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_time);
      return duration.count() / 1000;
    }

    //true代表运行时间到了
    bool TLE() {
      return std::chrono::duration_cast<std::chrono::milliseconds>(end_time -  std::chrono::high_resolution_clock::now()).count() < 0;
//      return getTime() > MAXTIME;
    }

    double getCostTime() {
      //return (clock() - start_time) / CLOCKS_PER_SEC;
      auto end = std::chrono::high_resolution_clock::now();
      auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start_time);
      return (double)duration.count() / 1000.0;
    }



  private:
    std::chrono::high_resolution_clock::time_point start_time, end_time;

    int MAXTIME;
  };
}
#endif //MKAHYPAR_TIMER_H
